package com.edu.chat.web.common.utils;

import cn.hutool.json.JSONUtil;
import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.DocumentParser;
import dev.langchain4j.data.document.parser.apache.tika.ApacheTikaDocumentParser;
import dev.langchain4j.data.document.splitter.DocumentSplitters;
import dev.langchain4j.data.segment.TextSegment;
import org.apache.tika.parser.AutoDetectParser;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;

/**
 * @program: KQAIA
 * @description: 文本切分工具类
 * @author: 无恙
 * @create: 2025-03-05 21:27
 **/

@Component
public class TextSplitterTool {

	private final DocumentParser documentParser = new ApacheTikaDocumentParser();

	// 最大切分长度， overlapSize
	@Value("${text.splitter.maxSegmentSize:1000}")
	private int maxSegmentSize = 1000;

	// overlapSize
	@Value("${text.splitter.overlapSize:100}")
	private int overlapSize = 100;


	/**
	 * 直接切分文本内容
	 *
	 * @param text 原始文本
	 * @return 切分后的文本段列表
	 */
	public List<TextSegment> splitText(String text) {
		Document document = Document.from(text);
		return DocumentSplitters.recursive(maxSegmentSize, overlapSize)
				.split(document);
	}

	/**
	 * pdf方式切分文本内容
	 *
	 * @param inputStream
	 * @return 切分后的文本段列表
	 */
	public List<TextSegment> splitTextByTika(InputStream inputStream) {
		DocumentParser parser = new ApacheTikaDocumentParser(AutoDetectParser::new, null, null, null, true);
		Document document = parser.parse(inputStream);
		return DocumentSplitters.recursive(maxSegmentSize, overlapSize)
				.split(document);
	}

	public static void main(String[] args) {
		TextSplitterTool textSplitterTool = new TextSplitterTool();

		try (InputStream inputStream = new FileInputStream("D:\\桌面\\作业\\RB软工数213 范铁凝202119244329.docx")) {
			List<TextSegment> textSegments = textSplitterTool.splitTextByTika(inputStream);
			for (TextSegment textSegment : textSegments) {
				System.out.println(textSegment.text());
				System.err.println(textSegment.metadata());
				// 提取总页数
				Integer npagesValue = textSegment.metadata().getInteger("xmpTPg:NPages");


				// 提取当前页数
				Integer indexValue = textSegment.metadata().getInteger("index");

				System.out.println("npagesValue:" + npagesValue);
				System.out.println("indexValue:" + indexValue);


			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


}
